//
//  TabMenuViewController.m
//  clublbiza
//
//  Created by WANG TOM on 7/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "TabMenuViewController.h"
#import "clublbizaAppDelegate.h"
#import "UIUnderlinedButton.h"


@interface TabMenuViewController ()

@end

@implementation TabMenuViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
       
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self refreshCatalogData];
}

- (void)viewDidUnload
{
    [super viewDidUnload];

   
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}
- (void) setCurrentCatalogAction:(UIButton*)sender
{
    AppMain *appMain = [AppMain shareInstance];
    appMain.currentSelectedMenuTabIndex = sender.tag;
   [self refreshCatalogData];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"currentSelectedMenuTabIndexChanged" object:self];
}
- (void) callLaterSendoutIndexChangeMessage
{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"currentSelectedMenuTabIndexChanged" object:self]; 
}
- (void)refreshCatalogData
{
    //Remove all item views
    NSArray *subviews = [[self.view subviews] copy];
    for (UIView *subview in subviews) {
        [subview removeFromSuperview];
    }
    [subviews release];
    
       
    int offset_x = 50;
    NSArray *data_list = [AppMain getTabBarMenuTable];
   
    UIButton *btn = nil;
    int menu_index = 0;
    for(NSString *menu_label in data_list){
        
        btn = [self createMenuButton:menu_index withLabel:menu_label offsetX:offset_x];
          [self.view addSubview:btn];
        
         offset_x += btn.frame.size.width + 100;
        menu_index++;
    }
    
    
    
    btn = [self createMenuButton:-1 withLabel:@"EVENTS" offsetX:offset_x];
    [self.view addSubview:btn];
    
    offset_x += btn.frame.size.width + 100;
    
    
    btn = [self createMenuButton:-2 withLabel:@"ADVERTISEMENT" offsetX:offset_x];
    [self.view addSubview:btn];
    
    offset_x += btn.frame.size.width + 100;
    
}
-(UIButton*) createMenuButton:(int)menu_tab_index withLabel:(NSString*)btnLabel offsetX:(int)offsetXValue
{
     AppMain *appMain = [AppMain shareInstance];
    if ( appMain.currentSelectedMenuTabIndex == -1 ) {
        appMain.currentSelectedMenuTabIndex = menu_tab_index;
        [NSTimer scheduledTimerWithTimeInterval: 1 target:self selector:@selector(callLaterSendoutIndexChangeMessage) userInfo:nil repeats:NO];
        
        
    }
    UIButton *btn = nil;
    if (  appMain.currentSelectedMenuTabIndex == menu_tab_index ) {
        btn = [UIUnderlinedButton buttonWithType:UIButtonTypeCustom];
        
        
        [btn setTitleColor:[UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
        
    } else {
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
       // [btn setTitleColor:[UIColor colorWithRed:20 green:20 blue:20 alpha:0.5] forState:UIControlStateNormal];
           [btn setTitleColor:[UIColor colorWithRed:1.0 green:0 blue:0 alpha:0.5] forState:UIControlStateNormal];
    }
    
    [btn addTarget:self action:@selector(setCurrentCatalogAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    CGSize sz = [btnLabel sizeWithFont:btn.titleLabel.font constrainedToSize:CGSizeMake(1000, 1000)];
    [btn setFrame:CGRectMake(offsetXValue,6,sz.width + 20,38)];
    [btn setTitle:btnLabel forState:UIControlStateNormal];
    
    
    
   
    
    btn.tag = menu_tab_index;
    
    return btn;
}
@end
